package https

import (
	"encoding/json"
	"net/http"
	"strings"
	"time"
)

// POST-JSON请求
//
//	url		请求地址
//	param	请求参数【如果不想封装外层map的话，可直接传入map[string]any{"_":xxx}(会在map最外层有且仅有_下标时才会直接取其内容进行传输)】
//	header	请求头，自动追加accept、Content-Type等属性
func PostJson(url string, param map[string]any, header map[string]string) *CURL {
	if param == nil {
		param = map[string]any{}
	}
	header = getHeader(header)
	curl := CURL{
		Uri:       url,
		ParamJson: param,
		Header:    header,
		StartTime: time.Now(),
	}
	defer questLogFunc(&curl)
	j, _ := json.Marshal(param)
	if v, ok := param["_"]; ok && len(param) == 1 {
		j, _ = json.Marshal(v)
	}
	req, err := http.NewRequest("POST", url, strings.NewReader(string(j)))
	if err != nil {
		curl.Error = err
		return &curl
	}
	return curl.queryBody(req)
}
